﻿namespace Cmn.Cls
{
    using Cmn;
    using System;
    using System.Text;
    using System.Web.UI;

    public class Pager
    {
        private Align _Align;
        private int _Index;
        private int _PageIndex;
        private int _PageSize;
        private string _StyleName;
        private int _TotalPage;
        private int _TotalRecord;
        private string _Url;
        private StringBuilder sbData;

        public Pager()
        {
            this._Index = 1;
            this.sbData = new StringBuilder();
            this._Url = "";
            this._PageIndex = 1;
            this._TotalPage = 1;
            this._PageSize = 20;
            this._TotalRecord = 0;
            this._StyleName = "BasePager";
            this._Align = Align.Left;
        }

        public Pager(string Url, int PageIndex, int TotalPage, int PageSize, int TotalRecord, string StyleName)
        {
            this._Index = 1;
            this.sbData = new StringBuilder();
            this._Url = Url;
            this._PageIndex = PageIndex;
            this._TotalPage = TotalPage;
            this._PageSize = PageSize;
            this._TotalRecord = TotalRecord;
            this._StyleName = StyleName;
        }

        public string getData(string CtrlID)
        {
            this._TotalPage = this._TotalRecord / this._PageSize;
            if ((this._TotalRecord % this._PageSize) != 0)
            {
                this._TotalPage++;
            }
            this.sbData.Remove(0, this.sbData.Length);
            this.sbData.Append("var myPager" + this._Index.ToString() + " = new " + this._StyleName + "();\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setUrl(\"" + this._Url + "\");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setPageIndex(" + this._PageIndex.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setTotalPage(" + this._TotalPage.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setPageSize(" + this._PageSize.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setTotalRecord(" + this._TotalRecord.ToString() + ");\n");
            string str = "";
            switch (this._Align)
            {
                case Align.Left:
                    str = "left";
                    break;

                case Align.Center:
                    str = "center";
                    break;

                case Align.Right:
                    str = "right";
                    break;
            }
            this.sbData.Append("myPager" + this._Index.ToString() + ".setAlign(\"" + str + "\");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".Execute(\"" + CtrlID + "\");\n");
            return this.sbData.ToString();
        }

        public void setData(Page Page, string CtrlID)
        {
            this._TotalPage = this._TotalRecord / this._PageSize;
            if ((this._TotalRecord % this._PageSize) != 0)
            {
                this._TotalPage++;
            }
            this.sbData.Remove(0, this.sbData.Length);
            this.sbData.Append("var myPager" + this._Index.ToString() + " = new " + this._StyleName + "();\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setUrl(\"" + this._Url + "\");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setPageIndex(" + this._PageIndex.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setTotalPage(" + this._TotalPage.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setPageSize(" + this._PageSize.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setTotalRecord(" + this._TotalRecord.ToString() + ");\n");
            string str = "";
            switch (this._Align)
            {
                case Align.Left:
                    str = "left";
                    break;

                case Align.Center:
                    str = "center";
                    break;

                case Align.Right:
                    str = "right";
                    break;
            }
            this.sbData.Append("myPager" + this._Index.ToString() + ".setAlign(\"" + str + "\");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".Execute(\"" + CtrlID + "\");\n");
            Js.Run(Page, this.sbData.ToString(), false, "Js" + this._Index.ToString());
        }

        public Align HAlign
        {
            set
            {
                this._Align = value;
            }
        }

        public int Index
        {
            set
            {
                this._Index = value;
            }
        }

        public int PageIndex
        {
            set
            {
                this._PageIndex = value;
            }
        }

        public int PageSize
        {
            set
            {
                this._PageSize = value;
            }
        }

        public string StyleName
        {
            set
            {
                this._StyleName = value;
            }
        }

        public int TotalPage
        {
            set
            {
                this._TotalPage = value;
            }
        }

        public int TotalRecord
        {
            set
            {
                this._TotalRecord = value;
            }
        }

        public string Url
        {
            set
            {
                this._Url = value;
            }
        }

        public enum Align
        {
            Left,
            Center,
            Right
        }
    }
}

